package com.linked.goods.goodsbase.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.goods.goodsbase.bean.dto.GoodsKindDTO;
import com.linked.goods.goodsbase.bean.po.GoodsKindPO;
import com.linked.goods.goodsbase.mapper.IGoodsKindMapper;
import com.linked.goods.goodsbase.service.IGoodsKindService;
import com.linked.universal.common.LinkedTree;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/5/19 9:26
 * @description : desc
 */
@Service
@Slf4j
public class GoodsTypeServiceImpl implements IGoodsKindService {

    private final IGoodsKindMapper goodsKindMapper;

    private final ObjectMapper mapper;

    public GoodsTypeServiceImpl(IGoodsKindMapper goodsKindMapper, ObjectMapper mapper) {
        this.goodsKindMapper = goodsKindMapper;
        this.mapper = mapper;
    }

    @Override
    public String addGoodsKind(GoodsKindPO param) throws Exception {
        if (!StringUtils.isEmpty(param.getKindPid())){
            GoodsKindPO pkind = new GoodsKindPO();
            pkind.setIfSonKind(1);
            pkind.setGoodsKindId(param.getKindPid());
            goodsKindMapper.updateById(pkind);
        }
        goodsKindMapper.insert(param);
        return param.getGoodsKindId();
    }

    @Override
    public LinkedTree<GoodsKindPO> queryKindTree(String kindPid) throws Exception {
        LinkedTree<GoodsKindPO> tmp = new LinkedTree<>();
        GoodsKindPO currentTree = goodsKindMapper.selectById(kindPid);
        tmp.setCurrentTree(currentTree);
        if (1 == currentTree.getIfSonKind()) {
            List<GoodsKindPO> trees = goodsKindMapper.selectSonTypes(kindPid);
            tmp.setSonTrees(trees);
        }
        return tmp;
    }
}
